<template>
  <div>
    <Searchbar>
      <el-input @change="getList" v-model="search.school_name" placeholder="请输入学校名称"></el-input>
      <el-button class="search-btn" @click="getList" type="primary">查询</el-button>
    </Searchbar>

    <TableBar>
      <el-button @click="onShowCreate" type="primary" icon="el-icon-plus">新建</el-button>
      <el-button @click="onShowUpload" icon="el-icon-upload">批量上传</el-button>
    </TableBar>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column fixed="left" type="index" width="55">序号</el-table-column>
      <el-table-column prop="school_id" label="学校id" ></el-table-column>
      <el-table-column prop="school_name" label="学校名称" ></el-table-column>
      <el-table-column prop="create_time" label="创建时间" ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="onShowEdit(scope.row)" size="small" class="text-default">编辑</el-button>
          <el-button v-if="scope.row.status == 'N'" type="text" @click="onDel(scope.row)" size="small" class="text-warning">停用</el-button>
          <el-button v-else type="text" @click="onDel(scope.row)" size="small" class="text-success">启用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change=handleSizeChange
      :current-page.sync="search.page"
      :page-size="search.page_size"
      :total="total"
    ></el-pagination>
    <EditSchool @onClose="onHideEdit" @afterSubmit="getList" :obj="editRow" :show="showEdit" />
    <MulUpload 
      action="/object/batch/create/"
      @onClose="onHideUpload"
      type=4
      title="导入学校"
      :temp="9"
      :show="showUpload" />
  </div>
</template>
<script>
import EditSchool from "@/modals/EditSchool";
import MulUpload from "@/components/MulUpload";
import showEditMixin from "@/mixins/showEditMixin";
import uploadMixin from "@/mixins/uploadMixin";
import pageMixin from "@/mixins/pageMixin";
import { schoolModel } from "@/api/admin_models";
import Searchbar from "@/components/Searchbar";
import TableBar from "@/components/TableBar";
import {Message} from 'element-ui';
export default {
  mixins: [showEditMixin, uploadMixin, pageMixin],
  components: { EditSchool, MulUpload, Searchbar, TableBar },
  data() {
    return {
      tableData: [],
      total: 0,
      search: {
        school_name: null,
        page: 1
      }
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      console.log("getList");
      schoolModel.objectList(
        this.search,
        res => {
          this.total = res.total_records;
          this.tableData = res.data.map(v => {
            return v;
          });
        },
        err => {
          console.log(err);
        }
      );
    },
    onDel(row) {
      let action = row.status == 'N' ? '停用' : '启用';
      this.$confirm(`确定${action}${row.school_name}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        schoolModel.update(row.school_id, {
          status: row.status == 'N' ? 'C' : 'N'
        }, res => {
          Message.success(res.message);
          this.getList();
        }, err => {
          Message.success(err.message);
        })
      }).catch((err) => {
        console.error(err)
        this.$message({
          type: 'info',
          message: '已取消操作'
        });          
      });
    }
  }
};
</script>
<style lang="scss">
</style>